﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace ch03
{
    public partial class Form2 : Form
    {
        //定义变量记录当前图片的下标
        int index = 0;
        //定义数组保存所有图片的名称
        //string[] picName = { "1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg", "6.jpg" };
        string[] picName;

        public Form2()
        {
            InitializeComponent();
        }

        //定义方法显示图片
        void showPic()
        {
            if (index < 0)
            {
                index = picName.Length - 1;
            }
            if (index > picName.Length - 1)
            {
                index = 0;
            }
            label1.Text = index + 1 + "/" + picName.Length;
            pictureBox1.Image = Image.FromFile(picName[index]);
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            //showPic();
        }
        //下一张图片的点击事件
        private void pictureBox3_Click(object sender, EventArgs e)
        {
            index++;
            showPic();
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            index--;
            showPic();
        }

        private void 选择文件夹ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //创建选择文件夹对话框对象
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.Description = "请选择图片所在文件夹：";
            fbd.SelectedPath = @"D:\CS架构编程\img";
            fbd.ShowNewFolderButton = false;
            //显示对话框
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                //获取用户选择的文件夹
                string path = fbd.SelectedPath;
                
                picName = Directory.GetFiles(path, "*.jpg");
                //picName = Directory.GetFiles(path).Where(s=>s.EndsWith(".jpg")).ToArray();
                //picName = Directory.GetFiles(path).Where(s=>s.EndsWith(".jpg")||s.EndsWith(".png")||s.EndsWith(".bmp")).ToArray();

                index = 0;

                showPic();

            }
        }
    }
}
